﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace BulldogCMS.Core.MVC
{
    // ~/BulldogCMS.Modules.TreeRenderer/Content.Site.css
    // ~/BulldogCMS.Modules.TreeRenderer/Views/Home/Index.cshtml

    public class ResourceFilesHelper
    {
        public Stream GetResourceStream(string fullResourcePath)
        {
            try
            {
                var parts = fullResourcePath.Split('/');
                var assemblyName = parts[1];
                parts[0] = ""; parts[1] = "";
                var resourceName = string.Join(".", parts).Substring(2);

                var assembly = Context.Application.Assemblies.Where(p => p.GetName().Name == assemblyName).FirstOrDefault();
                if (assembly != null)
                {
                    resourceName = string.Format("{0}.{1}", assemblyName, resourceName);
                    Stream result = assembly.GetManifestResourceStream(resourceName);
                    return result;
                }
            }
            catch { }
            throw new ApplicationException(string.Format("Resource {0} not found.", fullResourcePath));
        }

        public bool FileExists(string fullResourcePath)
        {
            try
            {
                var parts = fullResourcePath.Split('/');
                var assemblyName = parts[1];
                parts[0] = ""; parts[1] = "";
                var resourceName = string.Join(".", parts).Substring(2);

                var assembly = Context.Application.Assemblies.Where(p => p.GetName().Name == assemblyName).FirstOrDefault();
                if (assembly != null)
                {
                    resourceName = string.Format("{0}.{1}", assemblyName, resourceName);
                    object obj = assembly.GetManifestResourceNames();
                    bool result = assembly.GetManifestResourceNames().Contains(resourceName);
                    return result;
                }
                return false;
            } catch {
                return false;
            }
        }

        public bool IsModuleActive()
        {
            return (Context.Request != null && Context.Request.CurrentModuleAreaProvider != null);
        }

        // /Views/Home/Index.cshtml
        public bool FileExistsInCurrentModule(string fullResourcePath)
        {
            try
            {
                var assembly = Context.Request.CurrentModuleAreaProvider.CurrentProviderAssembly;
                if (assembly != null)
                {
                    object obj = assembly.GetManifestResourceNames();
                    fullResourcePath = fullResourcePath.TrimStart('~').Replace('/','.');
                    string resourceName = string.Format("{0}{1}", assembly.GetName().Name, fullResourcePath);
                    bool result = assembly.GetManifestResourceNames().Contains(resourceName);
                    return result;
                }
                return false;
            }
            catch
            {
                return false;
            }
        }


        public Stream GetModuleResourceStream(string fullResourcePath)
        {
            try
            {
                var assembly = Context.Request.CurrentModuleAreaProvider.CurrentProviderAssembly;
                if (assembly != null)
                {
                    fullResourcePath = fullResourcePath.TrimStart('~').Replace('/', '.');
                    string resourceName = string.Format("{0}{1}", assembly.GetName().Name, fullResourcePath);
                    Stream result = assembly.GetManifestResourceStream(resourceName);
                    return result;
                }
            }
            catch
            {

            }
            throw new ApplicationException(string.Format("Resource {0} not found.", fullResourcePath));
        }

        public bool FileExistsInWebProject()
        {
            return false;
        }

    }
}